<?php

namespace App\Http\Controllers\Pay;

use Illuminate\Http\Request;
use Session,Config;
use App\Http\Requests;
use App\Http\Service\Product\ProductService;
use App\Http\Service\User\UserService;
use App\Http\Service\Pay\PayService;
use App\Http\Controllers\Controller;

class PayController extends Controller
{

    public function __construct()
    {
        header("Content-type:text/html;charset=utf-8");
    }

    public function index(Request $request)
    {
        $title='支付';

        $where = [];
        $where['pay_id'] = $request->input('pay_id');
        $product = new ProductService();
        $info = $product->getProductInfo($where);

        if($info['code'] == '0')
        {
            $data = $info['data'];
        }

        return view('Pay.pay')->with('data',$data)->with('title',$title);
    }



    //  生成订单信息
    public function createOrder(Request $request)
    {
        $where = [];
        $price = 0;
        $oder_no = '';
        $token = '';
        $order = [];
        $coupon_discount = 1;      //   默认折扣为1
        $month = 1;                 //   数量  默认为一
        $where =  $request->all();


        /**
         * 给用户充值
         */
        if(!empty($where['account']))
        {
            $user = new UserService();
            $user_id =  $user->accountGetUserId($where);
            $order['to_user'] = $user_id;
        }else{
            unset($where['account']);
        }

        /**
         *优惠码优惠
         */
        if(!empty($where['code']))
        {
            $user = new UserService();
            $coupon =  $user->discountCoupon($where);
            if($coupon['code'] == '0')
            {
                $coupon_discount = $coupon['data']['coupon']['discount'];
                $order['coupon_code'] = $where['code'];                //  优惠号码
            }else{
                unset($where['code']);
            }

        }else{
            unset($where['code']);
        }


//        dd($where);
        //   商品数量
        $month = $where['month'];


        /**
         * 产品详情   价格
         */
        $product = new ProductService();

        $where['pay_id'] = $where['product'];
        unset($where['product']);


        $info = $product->getProductInfo($where);

        if ($info['code'] == 0) {
            $price = $info['data']['info']['price_discount'];
            $vip = $info['data']['info']['type_id'];
        }

        $token = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));              //  获取用户的token

        //  生成订单号
        $order_no = self::createUuid();

        //   订单信息
        $order['order_no'] = $order_no;
        $order['token'] = $token;
        $order['price'] = $price * $coupon_discount * $month;            //  价格
        $order['pay_id'] = $where['pay_id'];
        $order['month'] = $where['month'];
        $order['price_origin'] = $price * $month;
        $order['agent_id'] = 1;                                             //  牛魔科技   1
        $order['vip'] = $vip;                                               //  会员类型标识


        $pay = new PayService();
        $data = $pay->SlingAliPay($order);
        if(!$data['code']==0){
            echo $data['msg'];die();
        }else{
            return  $data['data'];
        }



    }


    //可以指定前缀
    static function createUuid($prefix = "")
    {
        $str = md5(uniqid(mt_rand(), true));
        $uuid  = substr($str,0,8) . '-';
        $uuid .= substr($str,8,4) . '-';
        $uuid .= substr($str,12,4) . '-';
        $uuid .= substr($str,16,4) . '-';
        $uuid .= substr($str,20,12);
        return $prefix . $uuid;
    }




}
